package org.datastealer.controller.advice;

import lombok.extern.slf4j.Slf4j;
import org.datastealer.exception.*;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：控制层的异常处理
 * @date ：Created in 2020/9/13
 */
@Slf4j
@ControllerAdvice("org.datastealer.controller")
public class ExceptionAdvice {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public DataStealerException handleException(Exception e) {
        if (e.getCause() instanceof SQLException) {
            log.error("调用DataStealer接口发生SQL执行异常，异常为：", e);
            return new DataStealerSqlException(e.getCause().getMessage());
        } else {
            log.error("调用DataStealer接口发生异常，异常为：", e);
            return new DataStealerCommonException(e.getCause().getMessage());
        }
    }

}